package com.cn.kabalifeuser.ui.my.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.cn.kabalifeuser.R;
import com.cn.kabalifeuser.base.IBaseActivity;
import com.cn.kabalifeuser.config.ShareUtil;
import com.cn.kabalifeuser.ui.integral.bean.ShoppingMalListBean;
import com.cn.kabalifeuser.ui.login.bean.MsgBean;
import com.cn.kabalifeuser.ui.my.adapter.PagerAdapter;
import com.cn.kabalifeuser.ui.my.bean.CouponListBean;
import com.cn.kabalifeuser.ui.my.bean.InviteFriendsBean;
import com.cn.kabalifeuser.ui.my.bean.MyUserInfoBean;
import com.cn.kabalifeuser.ui.my.bean.VipInfoBean;
import com.cn.kabalifeuser.ui.my.bean.VipRecommendBean;
import com.cn.kabalifeuser.ui.my.fragment.CouponFragments;
import com.cn.kabalifeuser.ui.my.fragment.VipRecommendFragments;
import com.cn.kabalifeuser.ui.my.fragment.VipRightsAndInterestsFragments;
import com.cn.kabalifeuser.utils.GlideUtil;
import com.cn.kabalifeuser.utils.RxToast;
import com.cn.kabalifeuser.view.PopupWindow.CommonPopupWindow;
import com.cn.kabalifeuser.view.PopupWindow.CommonUtil;
import com.flyco.roundview.RoundTextView;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by 晓林 on 2020/9/24.
 * function:
 */
public class VipActivity extends IBaseActivity<VipView, VipPresenter> implements VipView, OnTabSelectListener, CommonPopupWindow.ViewInterface {


    Bundle bundle;
    MyUserInfoBean myUserInfoBean;
    @BindView(R.id.img_vip)
    ImageView imgVip;
    @BindView(R.id.img_user_head)
    CircleImageView imgUserHead;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_user_vip_name)
    TextView tvUserVipName;
    @BindView(R.id.tv_privilege_num)
    RoundTextView tvPrivilegeNum;
    @BindView(R.id.tv_vip_time)
    TextView tvVipTime;
    @BindView(R.id.slitab)
    SlidingTabLayout slitab;
    @BindView(R.id.vp_type)
    ViewPager vpType;


    List<String> mList = new ArrayList<>();
    PagerAdapter mAdapter;
    private List<Fragment> mFragments = new ArrayList<>();
    private CommonPopupWindow popupWindowShare;
    int shareSign = 1;

    @Override
    protected VipPresenter createPresenter() {
        return new VipPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_vip;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);

        bundle = getIntent().getExtras();
        myUserInfoBean = (MyUserInfoBean) bundle.getSerializable("bean");
        GlideUtil.ImageLoad(getMe(), myUserInfoBean.getResult().getHead(), imgUserHead);
        tvUserName.setText(myUserInfoBean.getResult().getNickname());
        tvUserVipName.setText(myUserInfoBean.getResult().getUser_level());
        tvVipTime.setText(myUserInfoBean.getResult().getAfter_time());
        //todo 1：银卡，2：金卡，3:钻石
        if (myUserInfoBean.getResult().getUser_level_id() == 1) {
            imgVip.setImageResource(R.drawable.vip_bg_yin);
        } else if (myUserInfoBean.getResult().getUser_level_id() == 2) {
            imgVip.setImageResource(R.drawable.vip_bg_jin);
        } else if (myUserInfoBean.getResult().getUser_level_id() == 3) {
            imgVip.setImageResource(R.drawable.vip_bg_zhuan);
        } else {
            imgVip.setImageResource(R.drawable.vip_bg);
        }

        mList.add("推荐专区");
        mList.add("会员权益");
        mFragments.add(VipRecommendFragments.getInstance(0));
        mFragments.add(VipRightsAndInterestsFragments.getInstance(1));
        mAdapter = new PagerAdapter(getSupportFragmentManager(), mList, mFragments);
        vpType.setAdapter(mAdapter);
        vpType.setOffscreenPageLimit((mList.size() - 1));
        slitab.setViewPager(vpType);
        slitab.setOnTabSelectListener(this);
    }


    @OnClick({R.id.ll_btn_left, R.id.ll_btn_right, R.id.tv_btn_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_btn_left:
                finish();
                break;
            case R.id.ll_btn_right:
                if (myUserInfoBean != null)
                    showShare();
                break;
            case R.id.tv_btn_next:
                RxToast.success("请购买推荐专区即可自动升级vip");
                break;
        }
    }

    private void showShare() {
        if (popupWindowShare != null && popupWindowShare.isShowing()) return;
        View upView = LayoutInflater.from(getMe()).inflate(R.layout.pw_share, null);
        //测量View的宽高
        CommonUtil.measureWidthAndHeight(upView);
        popupWindowShare = new CommonPopupWindow.Builder(getMe())
                .setView(R.layout.pw_share)
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT, upView.getMeasuredHeight())
                .setBackGroundLevel(0.5f)//取值范围0.0f-1.0f 值越小越暗
                .setAnimationStyle(R.style.AnimUp)
                .setViewOnclickListener(this)
                .create();
        popupWindowShare.showAtLocation(findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);
    }

    @Override
    public void getChildView(View view, int layoutResId) {
        switch (layoutResId) {
            case R.layout.pw_share:
                view.findViewById(R.id.tv_wx).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UMWeb umWeb = new UMWeb(myUserInfoBean.getResult().getShare_address());
                        umWeb.setTitle("分享链接标题");
                        //描述
                        umWeb.setDescription("分享链接描述");
                        umWeb.setThumb(new UMImage(VipActivity.this, R.mipmap.ic_launcher));
//                            if (TextUtils.isEmpty(imageUrl)) {
//                                //本地缩略图
//                                umWeb.setThumb(new UMImage(activity, imageID));
//                            } else {
//                                //网络缩略图
//                                umWeb.setThumb(new UMImage(activity, imageUrl));
//                            }
                        ShareUtil.shareWXUrl(VipActivity.this, umWeb);
                        popupWindowShare.dismiss();
                    }
                });
                view.findViewById(R.id.tv_wx_p).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        UMWeb umWeb = new UMWeb(myUserInfoBean.getResult().getShare_address());
                        umWeb.setTitle("分享链接标题");
                        //描述
                        umWeb.setDescription("分享链接描述");
                        umWeb.setThumb(new UMImage(VipActivity.this, R.mipmap.ic_launcher));
                        ShareUtil.shareWXManyUrl(VipActivity.this, umWeb);
                        popupWindowShare.dismiss();

                    }
                });
                view.findViewById(R.id.tv_url).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        CopyToClipboard(getMe(), myUserInfoBean.getResult().getShare_address());
                        popupWindowShare.dismiss();

                    }
                });
                view.findViewById(R.id.tv_btn_next).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindowShare.dismiss();
                    }
                });
                break;
        }
    }

    @Override
    public void onTabSelect(int position) {

    }

    @Override
    public void onTabReselect(int position) {

    }

    @Override
    public void onReLoggedIn(String msg) {

    }

    @Override
    public void onError(String msg) {

    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onShoppingMalListSuccess(ShoppingMalListBean bean) {

    }


    @Override
    public void onVipInfoSuccess(VipInfoBean bean) {

    }
}
